<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-01-22 12:59:56
 * Desc:
 */

namespace app\base\service\WeChat;

use app\base\service\BaseService;
use app\core\exception\{BusinessException, ErrorCode};

class WeChatBaseService extends BaseService
{

    const DEFAULT_OFFICIAL_ACCOUNT_APP_ID = 'default_official_account';
    const DEFAULT_MINI_PROGRAM_APP_ID = 'default_mini_program';


    public static function getConfigByAppId($appId = '', $type = 'official_account')
    {

        $weChatConfigList = config('weChatConfigList');

        if (empty($weChatConfigList)) {
            throw new BusinessException(ErrorCode::COMMON_ERROR, '微信配置为空');
        }

        empty($appId) && $appId = ($type == 'official_account' ? self::DEFAULT_OFFICIAL_ACCOUNT_APP_ID : self::DEFAULT_MINI_PROGRAM_APP_ID);

        $config = $weChatConfigList[$appId] ?? [];

        if (empty($config)) {
            throw new BusinessException(ErrorCode::COMMON_ERROR, '未知的app_id');
        }

        return $config;
    }

}